package OutOfLine

import com.alibaba.fastjson.{JSON, JSONArray}
import org.apache.commons.lang.StringUtils
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

object FuGouLv {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName(s"${this.getClass.getName}")
      .master("local[*]")
      .getOrCreate()
    val sc = spark.sparkContext
    val logRDD: RDD[String] = sc.textFile("D:\\asa\\dianshang\\pay - 副本.log")

    val filter: RDD[((String, String, String), Int)] = logRDD.map(line => {
      val nObject = JSON.parseObject(line)
      //获取pay_status：1：成功，0：失败
      var pay_status = nObject.getString("pay_status")
      if (StringUtils.isEmpty(pay_status)) {
        pay_status = "0"
      }

      val openid = nObject.getString("openid")
      var pid =""
      var cid =""
      if (line.contains("goods")) {
        val goodsArr: JSONArray = nObject.getJSONArray("goods")
        if (goodsArr.size() > 0) {
          for (o <- 0 until goodsArr.size()) {
            val goodsArrObject = goodsArr.getJSONObject(o)
            //获取pid cid
            pid = goodsArrObject.getString("pid")
            cid = goodsArrObject.getString("cid")
          }
        }
      }
      (pay_status.toInt,pid,cid,openid)
    }).filter(x => (x._1 == 1)).map(x=>((x._2,x._3,x._4),1))

    val asd = filter.reduceByKey(_+_).count().toDouble

    val buy2_5 = filter.reduceByKey(_+_).filter(x=> (x._2>=2 && x._2<=5)).count().toDouble
    val buy5_ = filter.reduceByKey(_+_).filter(x=>x._2>5).count().toDouble
    println(buy2_5 / asd)
    println(buy5_ / asd)
    println(asd)
    //0.14285714285714285
    //0.0
    //7.0


    spark.stop()
  }
}
